package com.dd.ent.car.common.player;

import android.content.Context;
import android.util.AttributeSet;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

import com.dd.ent.car.R;


/**
 * Created by liujiansheng on 2016/8/11.
 * 播放器Loading界面
 */
public class MediaPlayerLoadingView extends ImageView{
    private boolean isLoading;
    private Animation mRotateAnimation;

    public MediaPlayerLoadingView(Context context) {
        super(context);
        init(context);
    }

    public MediaPlayerLoadingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MediaPlayerLoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        setImageResource(R.drawable.ic_loading);
        mRotateAnimation = createLoadingAnimation();
    }
    public void startLoading() {
        if (mRotateAnimation == null) {
            mRotateAnimation = createLoadingAnimation();
        }
        if (!isLoading) {
            isLoading = true;
            startAnimation(mRotateAnimation);
            setVisibility(VISIBLE);
        }
    }

    public void stopLoading() {
        if (isLoading) {
            isLoading = false;
            clearAnimation();
            setVisibility(GONE);
        }
    }

    private Animation createLoadingAnimation() {
        Animation rotateAnimation = new RotateAnimation(0f, 359f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setInterpolator(new LinearInterpolator());
        rotateAnimation.setDuration(1000);
        rotateAnimation.setRepeatCount(Animation.INFINITE);
        return rotateAnimation;
    }

    public boolean isLoading(){
        return isLoading;
    }
}
